/*
 * @Author: pandaman
 * @Date: 2024-02-22 09:42:20
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-11-11 18:00:45
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_cpp/include/xict_driver/task/gst_camera_driver_task.h
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#ifndef XICT_DRIVER_TASK_GST_CAMERA_DRIVER_TASK_H_
#define XICT_DRIVER_TASK_GST_CAMERA_DRIVER_TASK_H_

#include "memory"
#include "xict_driver/task/driver_base.h"
#include "xict_driver/task/periodic_task.h"
#include "xict_driver/utils/data_definition.h"
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>

namespace xict_driver {
    namespace task {
        class CameraDriverTask : public PeriodicTask {
        public:
            using Ptr = std::shared_ptr<CameraDriverTask>;

            CameraDriverTask(
                const xict_driver::utils::CameraDriverConfig& _config,
                xict_driver::task::PeriodicTaskManager* _task_manager);

            ~CameraDriverTask();

            /**
             * @brief Initialization 初始化
             */
            void Init() override;

            /**
             * @brief Run 运行
             */
            void Run() override;

            /**
             * @brief Clean up 清理
             */
            void Cleanup() override;

            /**
             * @brief Register data pub callback 注册数据发布回调函数
             *
             * @param _cb_data_pub
             */
            void RegisterDataPubCallback(
                const std::function<void(const std::string&, const cv::Mat&)>&
                    _cb_data_pub);
            /**
             * @brief reconnect camera
             *
             */
            bool Reconnect();

        private:
            xict_driver::utils::CameraDriverConfig config_;
            /// @brief 图像话题
            std::string image_topic_;
            /// @brief 图像
            cv::Mat image_;
            /// @brief 视频流
            cv::VideoCapture cap_;
            /// @brief 视频流地址
            std::string gst_source_ = "";
            /// @brief 图像上传回调函数
            std::function<void(const std::string&, const cv::Mat&)> cb_img_;
            /// @brief 允许的最大连续空帧数
            static constexpr int MAX_EMPTY_FRAMES = 5;
            int consecutive_empty_frames_         = 0;

            // cv::VideoWriter video_writer_;
        };
    }   // namespace task
}   // namespace xict_driver
#endif   // BRAINSYS_DRIVERS_INCLUDE_CAMERA_TASK_GST_CAMERA_DRIVER_TASK_H_
